package org.ccay.registry.util;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import org.ccay.core.registry.IRegistryManager;
import org.ccay.core.registry.RegistryItem;
import org.ccay.core.util.JsonUtil;
import org.ccay.registry.exception.RegistryParseException;

/**
 *
 */
public class RegistryQueryUtil {
	
	private static IRegistryManager registryManager;
	
	public void setRegistryManager(IRegistryManager registryManager) {
		RegistryQueryUtil.registryManager = registryManager;
	}

	/**
	 * 是否存在给定名称的注册项
	 * @param name
	 * @return
	 */
	public static boolean hasRegistryItem(String name){
		return registryManager.hasRegistryItem(name);
	}
	
	/**
	 * 获取给定名称的注册项
	 * @param name
	 * @return
	 */
	public static RegistryItem get(String name){
		return registryManager.get(name);
	}

	/**
	 * 获取值
	 * @param name
	 * @return
	 */
	public static String getValue(String name){
		return registryManager.getValue(name);
	}
	
	
	/**
	 * 获取值
	 * @param name
	 * @return
	 */
	public static Long getLongValue(String name){
		return registryManager.getLongValue(name);
	}
	
	/**
	 * 获取值 
	 * @param name
	 * @return
	 */
	public static Double getDoubleValue(String name){
		return registryManager.getDoubleValue(name);
	}
	
	/**
	 * 获取值
	 * @param name
	 * @return
	 */
	public static Boolean getBooleanValue(String name){
		return registryManager.getBooleanValue(name);
	}
	
	/**
	 * 获取值
	 * @param name
	 * @param dataformat
	 * @return
	 * @throws ParseException
	 */
	public static Date getDateValue(String name,DateFormat dataformat) throws ParseException{
		return registryManager.getDateValue(name,dataformat);
	}

	/**
	 * 获取值
	 * @param string
	 * @return
	 */
	public static Integer getIntegerValue(String string){
		return registryManager.getIntegerValue(string);
	}
	
	/**
	 * 获取值
	 * @param string
	 * @return
	 * @throws IOException 
	 */
	public static <T> T getObejct(String string,Class<T> clazz) {
		try {
			return JsonUtil.stringToObject(registryManager.getValue(string),clazz);
		} catch (IOException e) {
			throw new RegistryParseException(string,e);
		}
	}
}
